package notentool;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

/* TODO
 *   Add log function
 *   Add some methods
 *   Test the written methods
 */
public class Errors {
	private static ArrayList<Error> errorList = new ArrayList<Error>();
	private static Error lastError;
	private static boolean errorAddStopper=false; // to turn on and of add Errors.
	public static boolean isEmpty(){
		if ( null == errorList )
			return true;
		return errorList.isEmpty();
	}
	// the Error Objects for the ArrayList
	public static class Error {
		private String errorMsg;
		private int errorCode;
		private long localTime;
		private String errorOfFunction;
				
		public Error(String errorMsg, final int errorCode){
			this.errorCode=errorCode;
			this.errorMsg=errorMsg;
			this.localTime=System.currentTimeMillis();
		}
	}
	
	public static void stopAddErrors(){
		errorAddStopper=true;
	}
	public static void startAddErrors(){
		errorAddStopper=false;
	}
	
	/* return false if the error wasn't added */
	public static boolean addError(String errorMsg, int errorCode){
		if (null == errorList )
			return false;
		if( errorAddStopper )
			return false;
		Error e = new Error(errorMsg, errorCode);
		errorList.add(e);
		//NOTE: pretty cool, add the calling function and class name. But untested
		e.errorOfFunction=Thread.currentThread().getStackTrace()[2].toString();
		lastError=e;
		return true;
	}
		
	public static void printErrorList(){
		if (null == errorList )
			return;
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		
		
		long i=0;
		for( Error e : errorList){
			System.out.println("Fehlernr: " + i +
					"\n > Meldung: " + e.errorMsg +
					"\n > Fehlercode: " + e.errorCode +
					"\n > Zeitpunkt: " + e.localTime + " = " + sdf.format(new Date(e.localTime)) +
					"\n > Funktion: " + e.errorOfFunction );
			i++;
		}
	}
	
	public static void clearArrayList(){
		if (null == errorList )
			return;
		errorList.clear();
	}
	
	public static void printErrorById(int id){ // TODO add id out of range exception
		if (null == errorList )
			return;
		
		Error e = errorList.get(id);
		System.out.println("Fehlernr: " + id +
			"\n > Meldung: " + e.errorMsg +
			"\n > Fehlercode: " + e.errorCode +
			"\n > Zeitpunkt: " + e.localTime +
			"\n > Funktion: " + e.errorOfFunction);
	}
	
	/* returns -1 if the errorCode couldn't found
	 * returns the latest errorId which was found
	 * */
	public static int printErrorByErrorCode(int errorCode){
		for(int i=errorList.size()-1; 0<=i; i-- ){
			if( errorCode == errorList.get(i).errorCode ){
				printErrorById(i);
				return i;
			}
		}
		return -1;
	}
	
	
	public static void removeErrorById( int id){
		errorList.remove(id); // TODO add Try / Catch
		
		//return true;
	}
	
	/* returns false if the error couldn't found 
	 * returns true if the error is deleted */
	public static boolean removeErrorByErrorCode( int errorCode ){
		for( int i = errorList.size()-1; 0<=i; i-- ){
			if( errorCode == errorList.get(i).errorCode ){
				removeErrorById(i);
				return true;
			}
		}
		
		return false;
	}
	
	public static String getErrorMsgById(int id){
		if (null == errorList )
			return "Kein Fehler vorhanden!";
		return errorList.get(id).errorMsg;
	}
	
	public static int getErrorCodeById(int id){
		if (null == errorList )
			return -1;
		return errorList.get(id).errorCode;
	}
	
	public static long getErrorTimeById(int id){
		if (null == errorList )
			return 0;
		return errorList.get(id).localTime;
	}
	
	public static void printLastError(){
		if ( null == lastError )
			return;
		System.out.println(" > Meldung: " + lastError.errorMsg +
				"\n > Fehlercode: " + lastError.errorCode +
				"\n > Zeitpunkt: " + lastError.localTime +
				"\n > Funktion: " + lastError.errorOfFunction);
	}

	public static String getLastErrorMsg(){
		if ( null == lastError )
			return "Kein Fehler vorhanden!";
		return lastError.errorMsg;
	}
	
	public static int getLastErrorCode(){
		if ( null == lastError )
			return -1 ;
		return lastError.errorCode;
	}
	
	public static long getlastErrorTime(){
		if ( null == lastError )
			return 0 ;
		return lastError.localTime;
	}
}
